package com.my.geojson.test.wfs.controller;

import com.alibaba.fastjson.JSONObject;
import com.my.geojson.test.wfs.GeoToolsManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @author qinlei
 * @description todo
 * @date 2021/10/22 10:17
 */
@Slf4j
@RestController
@RequestMapping("/wfs")
public class WFSController {

    @Autowired
    private GeoToolsManager geoToolsManager;

//    http://localhost:9999/wfs/test:map/timo/4563215482120214
//{
//    "ip":"10.137.42.66",
//    "port":"8009",
//    "username":"admin",
//    "password":"geoserver",
//    "variables":{
//        "name":"支持1"
//    },
//    "params":{
//        "fid":"43"
//    }
//}
    @PutMapping("/{typeName}/{user}/{businessId}")
    public Object updateGeoserverData(@PathVariable String typeName,
                                         @PathVariable String user,
                                         @PathVariable String businessId,
                                         @RequestBody JSONObject jsonObject) {
        try {
            String ip = jsonObject.getString("ip");
            String port = jsonObject.getString("port");
            String username = jsonObject.getString("username");
            String password = jsonObject.getString("password");
            JSONObject variables = jsonObject.getJSONObject("variables");
            HashMap<String, Object> params = jsonObject.getObject("params", LinkedHashMap.class);
            geoToolsManager.insertUpdateByGeoWfs(businessId,user,ip,port,username,password,typeName,variables,params);
            return "Result.OK()";
        } catch (Exception e) {
            log.error("重试失败", e);
            return "Result.error(\"重试失败\")";
        }
    }
}
